package com.etc.controller;

import com.etc.util.RestTemplateUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.Map;

@Controller
@RequestMapping("/map")
public class MapController {

    private RestTemplateUtil restTemplateUtil = new RestTemplateUtil();

    private String ak = "1Ft3QXPFRTdpQgrFSBRXeX6SfznICmeO";


    @GetMapping("/addr")
    public @ResponseBody
    Object getAddrUserIp(HttpServletRequest request) throws URISyntaxException, UnknownHostException {
//        String ip = request.getHeader("x-forwarded-for");
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("Proxy-Client-IP");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("WL-Proxy-Client-IP");
//
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("HTTP_CLIENT_IP");
//
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
//
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getRemoteAddr();
//
//        }
//        String ip =request.getRemoteHost();
//        String ip =InetAddress.getLocalHost().getHostAddress();
//        System.out.println(ip);

        String ip="117.30.38.17";
        String url = "http://api.map.baidu.com/location/ip?ak=" + ak + "&ip=" + ip + "&coor=bd09ll"; //GET请求

        ResponseEntity<Map> forEntity = restTemplateUtil.restTemplate().getForEntity(new URI(url), Map.class);
        Map body = forEntity.getBody();

        return body;
    }
}
